//index.js
//获取应用实例
import http from '../http/api.js'
import time from '../../utils/util.js'
const app = getApp()
Page({
  data: { 
    indicatorDots: false,
    autoplay: false,
    interval: 5000,
    duration: 1000,   
    item:{},
    arrlist:[],//所以数据
    arrhd:[],//头部数据
    arrcont:[],//获取员工信息
    cid:"",
    MusicArr:[],//音乐
    ismusic:true,//音乐是否播放
    lyshow:false,//模态框 
    Phone:'',//手机号
  }, 
  onLoad: function (options) { 
    console.log(options) 
    this.setData({
      "cid": options.cid
    })
   
    var _this = this; 
    app.getUserall(function(userData){ 
      _this.setData({
        item:userData
      })  
    }) 
    _this.getmyCardS();
    _this.MyReserva();
    _this.getmyMusic();
    
  },
  //点击跳转到初始页
  cikckmycont:function(){
    wx.reLaunch({
      url: '../cardrecord/cardrecord'
    })
  },
  //点击音乐事件
  clickmuic:function(e){
    var isf = this.data.ismusic;
    if(isf){
      this.setData({
        "ismusic":false
      });
      this.audioCtx.pause() 
    }else{
      this.setData({
        "ismusic": true
      });
      this.audioCtx.play()
    }

  },
  //获取音乐
  getmyMusic:function(){
    var cid = this.data.cid
    app.http.POST("/MiniCardParameters/GetVisitMusicSet", { "cid": cid},(err,data)=>{
          console.log(data);
          if(data.code==1){
            this.setData({
              "MusicArr":data.data
            })
            if (data.data.MusicEnable==1){
                this.audioCtx.play();
            }
          }else{
            wx.showToast({
              title: '音乐获取失败',
              icon: 'none',
              duration: 2000
            })
          }
    })
  },
  //点击查看图片
  clickimgsdeilt:function(e){
    console.log(e);
    var index = e.currentTarget.dataset.index;
    var arr=this.data.arrlist;
    var imglist=[];
    arr.forEach(function(obj,i){
      if (obj.Func.FuncMark == "opus"){
        imglist = obj.FuncD.Opus;
      }
    }) 
    wx.previewImage({
      current: imglist[index], // 当前显示图片的http链接
      urls: imglist,
    }) 
  },
  //访问名片
  MyReserva:function(){
    var cid = this.data.cid
    app.http.POST("/MiniCardVisitRecord/VisitRecord", { "cid":cid},(err,data)=>{
        if(data.code==1){
            console.log("访问记录成功");
        }else{
          wx.showToast({
            title: '访问记录失败',
            icon: 'none',
            duration: 2000
          })
        }
    })
  },
  
  //获取首页信息
  getmyCardS:function(e){
    wx.showLoading({
      title: '加载中',
    })
    var cid = this.data.cid
    app.http.POST("/MiniCardCustomStyle/GetVisitCardStyle", { "cid": cid},(err,data)=>{
        if(data.code==1){
          var arr=data.data;
          var arrhd=[]
          arr= arr.filter(function (obj, i) {
            if (obj.FuncD.ServerWeek) {
              var ServerWeek = obj.FuncD.ServerWeek;
              ServerWeek = ServerWeek.split(",");
              obj.FuncD.ServerWeek = ServerWeek; 
            }
            if (obj.FuncD.ServerBTime) {
              obj.FuncD.ServerBTime = time.formatDate(obj.FuncD.ServerBTime,"hh:mm")
            }
            if (obj.FuncD.ServerETime) {
              obj.FuncD.ServerETime = time.formatDate(obj.FuncD.ServerETime, "hh:mm")
            } 
            if (obj.Func.FuncMark == "reserva") {
              arrhd.push(obj);
              return false;
            }
            if (obj.Func.FuncMark == "reward") {
              arrhd.push(obj);
              return false;
            }
            return true;
          }) 
          this.setData({
            "arrlist": arr,
            "arrhd": arrhd
          })
          console.log(this.data.arrlist)
          wx.hideLoading()
        }else{
          wx.showToast({
            title: '列表请求失败',
            icon: 'none',
            duration: 2000
          })
        }
    })
    app.http.POST("/MiniCardUser/GetVisitUserInfo", { "cid": cid},(err,data)=>{
       
        if(data.code==1){
            this.setData({
              "arrcont":data.data
            })
            wx.hideLoading()
        }else{
          wx.showToast({
            title: '员工信息请求失败',
            icon: 'none',
            duration: 2000
          })
        } 
    })  
  },
  clarlyer:function(){
    this.setData({
      "lyshow": false
    })
  },
  //点击打开弹窗
  clickleyr:function(e){
    var ipone = e.currentTarget.dataset.item;
    this.setData({
      "lyshow":true,
      "Phone": ipone
    })
  },
  //拨打电话
  clickCall:function(e){
    var ipone = this.data.Phone;
    wx.makePhoneCall({
      phoneNumber: ipone,
      success:function(res){
        this.setData({
          "lyshow": false
        })
      } 
    })
  },
  //保存电话号码
  clickPhone:function(e){
    var _this=this;
    var ipone = _this.data.Phone;
    wx.addPhoneContact({ 
      firstName: _this.data.arrcont.NickName,
      mobilePhoneNumber: ipone, 
      success:function(res){
        this.setData({
          "lyshow": false
        })
      }
    }) 
  },
  //复制微信号
  clickCopy:function(e){
    var wxnumber = e.currentTarget.dataset.item;
    wx.setClipboardData({
      data: wxnumber,
      success: function (res) { 
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功',
              icon: 'success',
              duration: 2000
            })
          }
        })
      }
    })
  },
  //查看路线
  clickway:function(e){
    var way = e.currentTarget.dataset.item;
    var idress = e.currentTarget.dataset.idress;
    if(!way){
      return;
    } 
    var arr=[];
    arr=way.split(",");
    var longitude = Number(arr[0]);
    var latitude = Number(arr[1]);
   // console.log(arr);
    wx.openLocation({
      latitude: latitude,
      longitude: longitude,
      address: idress,
      scale: 28
    });
 
  },
  //预约和打赏跳转
  Jump:function(e){
    console.log(e);
    var dataset = e.currentTarget.dataset;
    if (dataset.mark =="reserva"){
        wx.navigateTo({
            url: '../appointment/clientment/clientment?cid='+this.data.cid,
        });
    }
    if (dataset.mark == "reward") {

    }
  },
  /**
    * 生命周期函数--监听页面初次渲染完成
    */
  onReady: function (e) { 
    this.audioCtx = wx.createAudioContext('myAudio')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getmyCardS();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
      var arrcont = this.data.arrcont;
      return {
          title: arrcont.NickName + "的美名片",
          path: "/pages/index/index?cid="+this.data.cid,
          success: function (res) { 
              wx.showToast({
                  title: '分享成功',
                  icon: "success",
                  duration: 200
              })
          }
      }
  }
})